package com.maven.zh.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.maven.zh.bean.Datas;
import com.maven.zh.bean.Manager;
import com.maven.zh.bean.Page;
import com.maven.zh.bean.Permission;
import com.maven.zh.dao.ManagerDao;
import com.maven.zh.service.ManagerService;



@Service
public class ManagerServiceImpl implements ManagerService{

	@Autowired
	private ManagerDao managerDao;

	public Manager queryManager4Login(Manager manager) {
		return managerDao.selectManager4Login(manager);
	}

	public List<Permission> queryPermissionsByManager(Manager loginManager) {
		return managerDao.queryPermissionsByManager(loginManager);
	}

	public void deleteBatManagers(Datas ds) {
		managerDao.deleteBatManagers(ds);
	}

	public int deleteManagerById(Integer id) {
		return managerDao.deleteManagerById(id);
	}

	public Page<Manager> queryPage(Integer pageno, Integer pagesize, String qname) {
		// 获取分页对象
				Page<Manager> page = new Page<Manager>();
				
				Map<String, Object> paramMap = new HashMap<String, Object>();
				paramMap.put("start", (pageno-1)*pagesize);
				paramMap.put("pagesize", pagesize);
				paramMap.put("qname", qname);
				// 查询用户数据
				List<Manager> managers = managerDao.queryManager4Page(paramMap);
				
				// 查询用户数量
				int count = managerDao.queryManagerCount4Page(paramMap);
				
				page.setDatas(managers);
				page.setTotalsize(count);
				
				// 计算数据总页码
				int totalno = 0;
				if ( count % pagesize == 0 ) {
					totalno = count / pagesize;
				} else {
					totalno = count / pagesize + 1;
				}
				page.setTotalno(totalno);
				
				return page;
	}

	public Manager queryManagerById(Integer id) {
		return managerDao.queryManagerById(id);
	}

	public int updateManager(Manager manager) {
		return managerDao.updateManager(manager);
	}

	public void insertManager(Manager manager) {
		managerDao.insertManager(manager);
		
	}

	public List<Integer> queryRoleIdsByManagerId(Integer id) {
		return managerDao.queryRoleIdsByManagerId(id);
	}

	public void assignRoles(Integer managerid, Datas ds) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("managerid", managerid);
		for ( Integer roleid : ds.getIds() ) {
			paramMap.put("roleid", roleid);
			managerDao.insertManagerRoles(paramMap);
		}
		
	}
	public void cancleAssignRoles(Integer userid, Datas ds) {
		Map<String, Object> paramMap = new HashMap<String, Object>();
		
		paramMap.put("managerid", userid);
		paramMap.put("roleids", ds.getIds());
		managerDao.deleteManagerRoles(paramMap);
	}

}
